package com.cpi.cornpp.instructionsattachment.converter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.cpi.cornpp.instructionsattachment.dto.InstructionAttachmentDTO;
import com.cpi.cornpp.instructionsattachment.view.InstructionAttachmentView;
import com.cs.common.utils.FileExtensionUtils;

/**
 * 
 * @author Administrador
 *
 */
public class InstructionAttachmentConverter {

	/**
	 * 
	 * @param instructionAttachmentDTO
	 * @return
	 */
	public final InstructionAttachmentView convert(InstructionAttachmentDTO instructionAttachmentDTO){
												   
		InstructionAttachmentView instructionAttachmentView = null;
		
		if (instructionAttachmentDTO != null){
			instructionAttachmentView = new InstructionAttachmentView();			
			instructionAttachmentView.setInstructionAttachmentId(instructionAttachmentDTO.getInstructionAttachmentId());
			instructionAttachmentView.setPurchaseInstructionDetailId(instructionAttachmentDTO.getPurchaseInstructionDetailId());
			instructionAttachmentView.setDescription(instructionAttachmentDTO.getDescription());
			instructionAttachmentView.setPath(instructionAttachmentDTO.getPath());
			instructionAttachmentView.setFileExtension(FileExtensionUtils.getFileExtension(instructionAttachmentDTO.getPath()));
		}
		
		return instructionAttachmentView;
	}
	
	/**
	 * 
	 * @param instructionAttachmentView
	 * @return
	 */
	public final InstructionAttachmentDTO convert(InstructionAttachmentView instructionAttachmentView){
		InstructionAttachmentDTO instructionAttachmentDTO = null;		
		if (instructionAttachmentView != null){
			instructionAttachmentDTO = new InstructionAttachmentDTO();
			
			instructionAttachmentDTO.setInstructionAttachmentId(instructionAttachmentView.getInstructionAttachmentId());
			instructionAttachmentDTO.setPurchaseInstructionDetailId(instructionAttachmentView.getPurchaseInstructionDetailId());
			instructionAttachmentDTO.setDescription(instructionAttachmentView.getDescription());
			instructionAttachmentDTO.setPath(instructionAttachmentView.getPath());

		}
		
		return instructionAttachmentDTO;
	}
	
	/**
	 * 
	 * @param instructionAttachementViews
	 * @return
	 */
	public final List convertViews(List instructionAttachementViews){
		List instructionAttachements = null;
		instructionAttachements = new ArrayList(0);
		Iterator iterator = instructionAttachementViews.iterator();
		InstructionAttachmentView view = null;		
		if (!CollectionUtils.isEmpty(instructionAttachementViews)){
			while( iterator.hasNext() ){
				view = (InstructionAttachmentView)iterator.next();
				instructionAttachements.add(convert(view));
			}
		}
		
		return instructionAttachements;
	}
	
	/**
	 * 
	 * @param instructionAttachementDTO
	 * @return
	 */
	public final List convertDTOs(Collection instructionAttachementDTO){
		List instructionAttachements = null;
		instructionAttachements = new ArrayList(0);
		Iterator iterator = instructionAttachementDTO.iterator();
		InstructionAttachmentDTO dto = null;
		if (!CollectionUtils.isEmpty(instructionAttachementDTO)){
			while(iterator.hasNext()){
				dto = (InstructionAttachmentDTO)iterator.next();
				instructionAttachements.add(convert(dto));
			}
		}
		
		return instructionAttachements;
	}
}
